<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.9.2"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>GrPPI: Stream reduction pattern</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="logo.svg"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">GrPPI
   &#160;<span id="projectnumber">1.0</span>
   </div>
   <div id="projectbrief">Generic and Reusable Parallel Pattern Interface</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.2 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search','.html');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

</div><!-- top -->
<div class="PageDoc"><div class="header">
  <div class="headertitle">
<div class="title">Stream reduction pattern </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>The <b>stream reduction</b> pattern consists on the use of the reduce pattern over a stream of data. Each element is processed using a reduce operator, that generates a new output element. The output elements are sent to an output stream. This streaming pattern can only be used inside another pattern and consequently does not take an execution policy itself, but uses the execution policy of its enclosing pattern.</p>
<p>The interface to the <b>stream reduction</b> pattern is provided by function <code>grppi::stream_reduce()</code>.</p>
<div class="fragment"><div class="line"><a class="code" href="group__pipeline__pattern.html#gaf3942c499118a951617b95c4ee10a502">grppi::pipeline</a>(ex</div>
<div class="line">  stage1,</div>
<div class="line">  grppi::stream_reduce(arguments...),</div>
<div class="line">  stage2,</div>
<div class="line">  stage3,</div>
<div class="line">  ...)</div>
<div class="ttc" id="agroup__pipeline__pattern_html_gaf3942c499118a951617b95c4ee10a502"><div class="ttname"><a href="group__pipeline__pattern.html#gaf3942c499118a951617b95c4ee10a502">grppi::pipeline</a></div><div class="ttdeci">void pipeline(const Execution &amp;ex, Generator &amp;&amp;generate_op, Transformers &amp;&amp;... transform_ops)</div><div class="ttdoc">Invoke Pipeline pattern on a data stream.</div><div class="ttdef"><b>Definition:</b> pipeline.h:46</div></div>
</div><!-- fragment --><h1><a class="anchor" id="autotoc_md709"></a>
Stream reduction variants</h1>
<ul>
<li><em>Composable stream reduce</em>: Defines a stream reduce that can be used as a building block by another pattern (e.g. a pipeline).</li>
</ul>
<h1><a class="anchor" id="autotoc_md710"></a>
Key elements in stream reduction</h1>
<p>The key elements in a <b>stream reduction</b> is the <b>Combiner</b> operation, the <b>window size</b> and the <b>offset</b>.</p>
<p>A <b>Combiner</b> is any C++ callable entity, that is able to combine two values into a single value. A <b>Combiner</b> <code>cmb</code> is any operation taking two values <code>x</code> and <code>y</code> of types <code>T</code> and <code>U</code> and returning a combined value of type <code>T</code>, making valid the following:</p>
<div class="fragment"><div class="line">T x;</div>
<div class="line">U y;</div>
<div class="line">T res = cmb(x,y);</div>
</div><!-- fragment --><p>A <b>window size</b> is an integer value that defines the number of elements that should be collapsed to produce a resulting value.</p>
<p>An <b>offset</b> is an integer value that defines the overlapping degree among windows.</p>
<h1><a class="anchor" id="autotoc_md711"></a>
Details on stream reduction variants</h1>
<h2><a class="anchor" id="autotoc_md712"></a>
Composable stream reduction</h2>
<p>A composable stream reduction has four elements:</p>
<ul>
<li>A <b>window size</b>.</li>
<li>An <b>offset</b>.</li>
<li>An <b>identity value</b> provided as an input value.</li>
<li>A <b>Combiner</b> combining two values into a single value.</li>
</ul>
<p>The input values will be generated by the upper level pattern, which will be also responsible for consuming the output values.</p>
<hr  />
<p> <b>Example</b>: A stream reduction stage in a pipeline. </p><div class="fragment"><div class="line"><a class="code" href="group__pipeline__pattern.html#gaf3942c499118a951617b95c4ee10a502">grppi::pipeline</a>(exec,</div>
<div class="line">  stageA,</div>
<div class="line">  stageB,</div>
<div class="line">  grppi::stream_reduce(</div>
<div class="line">    window_size, offset, 0,</div>
<div class="line">    [](<span class="keywordtype">int</span> x, <span class="keywordtype">int</span> y) { <span class="keywordflow">return</span> x+y; }),</div>
<div class="line">  stageC</div>
<div class="line">  );</div>
</div><!-- fragment --> <hr  />
<p> <b>Note</b>: For brevity we do not show here the details of other stages.</p>
<p>For composing complex patterns, the <b>stream_reduce()</b> function may be used to create an object that may be supplied to another pattern to build a composed pattern.</p>
<hr  />
<p> <b>Example</b>: A composable stream reduction stage in a pipeline. </p><div class="fragment"><div class="line"><span class="keyword">auto</span> chunk_sum = grppi::stream_reduce(</div>
<div class="line">  window_size, offset, 0,</div>
<div class="line">  [](<span class="keywordtype">int</span> x, <span class="keywordtype">int</span> y) { <span class="keywordflow">return</span> x+y; });</div>
<div class="line"> </div>
<div class="line"><a class="code" href="group__pipeline__pattern.html#gaf3942c499118a951617b95c4ee10a502">grppi::pipeline</a>(exec,</div>
<div class="line">  stageA,</div>
<div class="line">  stageB,</div>
<div class="line">  chunk_sum,</div>
<div class="line">  stageC</div>
<div class="line">  );</div>
</div><!-- fragment --> <hr  />
<p> <b>Note</b>: For brevity we do not show here the details of other stages. </p>
</div></div><!-- contents -->
</div><!-- PageDoc -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by&#160;<a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.2
</small></address>
</body>
</html>
